from django.shortcuts import render

from django.views.generic.base import View
# Create your views here.
from .models import Device,DeviceStatus
from option_users.models import Operator,OptionLog
from .forms import DeviceLogForm
class DeviceView(View):

    def get(self,request,device_id):
        device = Device.objects.get(pk=device_id)
        devicestatus =DeviceStatus.objects.all()
        forms = DeviceLogForm()
        return render(request,"device.html",{"device":device,'forms':forms,'devicestatus':devicestatus})

    def post(self,request):

        forms= DeviceLogForm(request.POST)
        device_id = request.POST.get("device_id")
        # if forms.is_valid():


        employee_id = request.POST.get('employee_id')
        password = request.POST.get('password')
        devicestatus_id = request.POST.get('devicestatus')

        try:
            operator = Operator.objects.get(employee_id=employee_id,password=password)
            device = Device.objects.get(pk=device_id)
            devicestatus=DeviceStatus.objects.get(pk=devicestatus_id)
            operator_log=OptionLog()
            operator_log.device=device
            operator_log.devicestatus=devicestatus
            operator_log.operator=operator
            operator_log.save()
            return render(request,"success.html",{'operator_log':operator_log})
        except Operator.DoesNotExist:
            device =Device.objects.get(pk=device_id)
            devicestatus = DeviceStatus.objects.all()
            forms = DeviceLogForm()
            return render(request,"device.html",{"device":device,'forms':forms,'devicestatus':devicestatus,'error':'工号或密码错误'})


from xadmin.views import BaseAdminView

class BarcodeView(BaseAdminView):
    template_name = "barcode.html"

    def get(self,request,*args, **kwargs):
        barcode = Device.objects.all()
        return  render(request,self.template_name,{'barcode':barcode})